package com.aacar.service.impl;

import java.util.Map;

import com.aacar.constant.ConsFiled;
import com.aacar.utils.Pphconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aacar.dao.VesionDao;
import com.aacar.dto.DataDto;
import com.aacar.dto.VersionDto;
import com.aacar.entity.DataVersion;
import com.aacar.entity.Version;
import com.aacar.service.VesionService;
import com.aacar.vo.DataVersionVo;
import com.aacar.vo.VersionUrlVo;
import com.aacar.vo.VersionVo;
import com.google.common.collect.Maps;

//版本服务
@Repository("vesionService")
public class VesionServiceImpl implements VesionService{
	
	@Autowired
	private VesionDao vesionDao;
	
	@Override
	public VersionUrlVo getNewVersion(VersionDto versionDto) {
		Version v = new Version();
		v.setAppType(versionDto.getAppType());
		VersionVo version = this.vesionDao.getNewVersion(v);
		VersionUrlVo rv = new VersionUrlVo();
		try {
			int result = compareVersion(version.getVersion(),versionDto.getVersion());
			//返回新版本大于当前版本,则需要更新
			if(result > 0) {
				rv.setFlag("1");//需要更新
				if(version.getUrl()!=null) {
					if("andriod_aacar_client".equals(versionDto.getAppType())) {
						rv.setUrl(Pphconfig.getContextProperty(ConsFiled.ROOT_PATH) + version.getUrl());
					}else if("ios_aacar_client".equals(versionDto.getAppType())){
						rv.setUrl(version.getUrl());
					}
				}
				rv.setDetail(version.getDetail());
				rv.setForce(version.getForce());
			}else {
				rv.setFlag("0");//需要更新
				rv.setDetail("");
				rv.setForce("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rv;
	}
	
	//比较版本号
	public  int compareVersion(String version1, String version2) throws Exception {  
	    if (version1 == null || version2 == null) {  
	        throw new Exception("compareVersion error:illegal params.");  
	    }  
	    String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配，不能用"."；  
	    String[] versionArray2 = version2.split("\\.");  
	    int idx = 0;  
	    int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值  
	    int diff = 0;  
	    while (idx < minLength  
	            && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度  
	            && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符  
	        ++idx;  
	    }  
	    //如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；  
	    diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;  
	    return diff;  
	}
	
	//检测数据版本
	public Map<String,Object> checkDataVersion(DataDto dataDto) {
		Map<String,Object> resultMap = Maps.newConcurrentMap();
		DataVersion dataVersion = new DataVersion();
		dataVersion.setTableName(dataDto.getTableName());
		DataVersionVo dvv = this.vesionDao.getDataVersion(dataVersion);
		try {
			int result = compareVersion(dvv.getVersion(),dataDto.getVersion());
			//返回新版本大于当前版本,则需要更新
			if(result > 0) {
				resultMap.put("flag", "1");
				resultMap.put("info", dvv.getDetail());
				resultMap.put("version", dvv.getVersion());
			}else {
				resultMap.put("flag", "0");
				resultMap.put("version", dvv.getVersion());
				resultMap.put("info", "无需更新.");
			}
		}catch (Exception e) {
			resultMap.put("flag", "0");
			resultMap.put("version", dvv.getVersion());
			resultMap.put("info", "无需更新.");
		}
		return resultMap;
	}
	
	//获取数据初始版本
	public DataVersionVo getDataVersion(DataDto dataDto) {
		DataVersion dataVersion = new DataVersion();
		dataVersion.setTableName(dataDto.getTableName());
		return this.vesionDao.getDataVersion(dataVersion);
	}  

}
